package com.tengju.user.application.sms;

import com.tengju.user.domain.model.sms.*;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.service.SmsBaseService;
import com.tengju.user.domain.shared.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class SmsBizService {

    private SmsBaseService smsBaseService;

    @Autowired
    public SmsBizService(SmsBaseService smsBaseService) {
        this.smsBaseService = smsBaseService;
    }

    /**
     * 发送短信验证码
     */
    public boolean sendSmsVerifyCode(SmsVerifyCode smsVerifyCode) {

        log.info("手机号：{},验证码：{}", smsVerifyCode.getMobile(), smsVerifyCode.getVerifyCode());

        return smsBaseService.sendText(smsVerifyCode.getTempId(),
                smsVerifyCode.getAreaCode(), smsVerifyCode.getMobile(), smsVerifyCode.getVerifyCode());

    }

    //==============================================签约短信==============================================

    /**
     * 发送签约邀约短信
     */
    public void sendSmsSignInvite(String mobile, StarLevelEnum starLevel) {

        SmsSignInvite smsSignInvite = SmsTempFactory.smsSignInvite(mobile, starLevel);

        log.info("手机号：{},邀约等级：{}", smsSignInvite.getMobile(), smsSignInvite.getStarLevel().getDesc());

        smsBaseService.sendText(smsSignInvite.getTempId(),
                "", smsSignInvite.getMobile(), smsSignInvite.getStarLevel().getDesc());

    }

    /**
     * 发送签约成功短信
     */
    public void sendSmsSignSuccess(String mobile, StarLevelEnum starLevel, LocalDateTime expireTime) {

        SmsSignSuccess signSuccess = SmsTempFactory.smsSignSuccess(mobile, starLevel, expireTime);

        String formatExpireTime = DateUtil.formartLocalDateTime(DateUtil.DATE_DAY_FORMAT, signSuccess.getExpireTime());

        log.info("手机号：{},加入等级：{}，过期时间：{}", signSuccess.getMobile(), signSuccess.getStarLevel().getDesc(), formatExpireTime);

        String smsContext = signSuccess.getStarLevel().getDesc() + "," + formatExpireTime;

        smsBaseService.sendText(signSuccess.getTempId(),
                "", signSuccess.getMobile(), smsContext);

    }

    /**
     * 签约审批通过短信
     */
    public void sendSignAuditSuccessMsg(String mobile) {
        SmsSignAuditSuccess signAuditSuccess = SmsTempFactory.smsSignAuditSuccess(mobile);

        log.info("手机号：{}, 签约审批通过", signAuditSuccess.getMobile());

        smsBaseService.sendText(signAuditSuccess.getTempId(),
                "", signAuditSuccess.getMobile(), "");

    }

    /**
     * 签约审批拒绝短信
     */
    public void sendSignAuditFailMsg(String mobile) {

        SmsSignAuditFail signAuditFail = SmsTempFactory.smsSignAuditFail(mobile);

        log.info("手机号：{}, 签约审批拒绝", signAuditFail.getMobile());

        smsBaseService.sendText(signAuditFail.getTempId(),
                "", signAuditFail.getMobile(), "");

    }

    /**
     * 申请不通过短信通知
     */
    public void sendApplyVerifyReject(String mobile) {


        SmsChargeApplyReject smsChargeApplyReject = SmsTempFactory.smsChargeApplyReject(mobile);

        smsBaseService.sendText(smsChargeApplyReject.getTempId(), "", smsChargeApplyReject.getMobile(), "");
    }

    /**
     * 初审未通过短信
     */
    public void sendChargeEarlyReject(String mobile) {

        SmsChargeEarlyApplyReject smsChargeEarlyApplyReject = SmsTempFactory.smsChargeEarlyApplyReject(mobile);

        smsBaseService.sendText(smsChargeEarlyApplyReject.getTempId(), "", smsChargeEarlyApplyReject.getMobile(), "");

    }

    /**
     * 初审通过短信
     *
     * @param mobile
     */
    public void sendChargeEarlyPass(String mobile) {

        SmsChargeEarlyApplyPass smsChargeEarlyApplyPass = SmsTempFactory.smsChargeEarlyApplyPass(mobile);

        smsBaseService.sendText(smsChargeEarlyApplyPass.getTempId(), "", smsChargeEarlyApplyPass.getMobile(), "");

    }

    /**
     * 复审未通过
     *
     * @param mobile
     */
    public void sendChargeRecheckReject(String mobile) {

        SmsChargeRecheckReject smsChargeRecheckReject = SmsTempFactory.smsChargeRecheckReject(mobile);

        smsBaseService.sendText(smsChargeRecheckReject.getTempId(), "", smsChargeRecheckReject.getMobile(), "");

    }

    /**
     * 解绑收款账户
     *
     * @param mobile
     */
    public void sendUnbindAccount(String mobile) {

        SmsUnbindAccount smsUnbindAccount = SmsTempFactory.smsUnbindAccount(mobile);

        smsBaseService.sendText(smsUnbindAccount.getTempId(), "", smsUnbindAccount.getMobile(), "");

    }

    /**
     * 合作方式变更
     * @param mobile
     */
    public void sendAccountChangeMode(String mobile,String newModeName,int autoCancelNum) {

        SmsAccountChangeMode smsAccountChangeMode = SmsTempFactory.smsAccountChangeMode(mobile,newModeName,autoCancelNum);

        String smsContext = newModeName + "," + autoCancelNum;

        smsBaseService.sendText(smsAccountChangeMode.getTempId(), "", smsAccountChangeMode.getMobile(), smsContext);

    }

    /**
     * 服务费发放成功
     * @param mobile
     */
    public void sendChargeDistributeSuccess(String mobile,String bankNo) {

        SmsChargeDistributeSuccess smsChargeDistributeSuccess = SmsTempFactory.smsChargeDistributeSuccess(mobile,bankNo);

        String smsContext = bankNo;

        smsBaseService.sendText(smsChargeDistributeSuccess.getTempId(), "", smsChargeDistributeSuccess.getMobile(), smsContext);

    }


    /**
     * 服务费发放失败
     * @param mobile
     */
    public void sendChargeDistributeFail(String mobile) {

        SmsChargeDistributeFail smsChargeDistributeFail = SmsTempFactory.smsChargeDistributeFail(mobile);

        smsBaseService.sendText(smsChargeDistributeFail.getTempId(), "", smsChargeDistributeFail.getMobile(), "");

    }

    /**
     * 认证审核失败
     * @param mobile
     */
    public void sendAccountAuthFail(String mobile,String authModeName, String remark) {

        SmsAccountAuthFail smsAccountAuthFail = SmsTempFactory.smsAccountAuthFail(mobile,authModeName,remark);

        String smsContext = authModeName + "," + remark;

        smsBaseService.sendText(smsAccountAuthFail.getTempId(), "", smsAccountAuthFail.getMobile(), smsContext);

    }


    /**
     * 批量付款验证码
     * @param mobile
     */
    public void sendChargeBatchPayment(String mobile,String name, String code) {

        SmsChargeBatchPayment smsChargeBatchPayment = SmsTempFactory.smsChargeBatchPayment(mobile,name,code);

        String smsContext = name + "," + code;

        smsBaseService.sendText(smsChargeBatchPayment.getTempId(), "", smsChargeBatchPayment.getMobile(), smsContext);

    }

    /**
     * 经销商即将晋升提醒短信
     * @param mobile 手机号
     * @param lackNum 还缺少的数量
     * @param lackName "经销商" 或 "客户"
     */
    public boolean starUpcomingNotice(String mobile,Integer lackNum, String lackName) {

        SmsStarUpcomingNotice smsStarUpcomingNotice = SmsTempFactory.smsStarUpcomingNotice(mobile);

        String smsContext = lackNum + "," + lackName;

        return smsBaseService.sendText(smsStarUpcomingNotice.getTempId(), "", smsStarUpcomingNotice.getMobile(), smsContext);

    }



}
